package com.weihui.jxc.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.weihui.jxc.common.CommonFactory;
import com.weihui.jxc.model.Operator;
import com.weihui.jxc.service.OperatorService;

import javax.swing.JButton;
import javax.swing.JFrame;
/**
 * 更改密码
 * @author FPF
 *
 */
public class ChangePwdInternalFrame extends JInternalFrame {
	private JTextField usernameTf;
	private JTextField oldpwdTf;
	private JTextField newPwdTf;
	private JTextField repwdTf;
	private String username;//登录名
	private OperatorService operatorService;

	/**
	 * Create the frame.
	 */
	public ChangePwdInternalFrame() {
		super("更改密码", true, true, true, true);
		setBounds(100, 100, 450, 300);
		
		JPanel panel = new JPanel();
		
		JPanel panel_1 = new JPanel();
		
		JLabel label = new JLabel("旧  密  码：");
		panel_1.add(label);
		
		oldpwdTf = new JTextField();
		oldpwdTf.setColumns(20);
		panel_1.add(oldpwdTf);
		
		JPanel panel_2 = new JPanel();
		
		JLabel label_1 = new JLabel("新  密  码：");
		panel_2.add(label_1);
		
		newPwdTf = new JTextField();
		newPwdTf.setColumns(20);
		panel_2.add(newPwdTf);
		
		JPanel panel_3 = new JPanel();
		
		JLabel label_2 = new JLabel("确认密码：");
		panel_3.add(label_2);
		
		repwdTf = new JTextField();
		repwdTf.setColumns(20);
		panel_3.add(repwdTf);
		
		JPanel panel_4 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(panel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(21, Short.MAX_VALUE))
		);
		
		JButton updateBtn = new JButton("修改");
		//updateBtn.addActionListener(this);//给登录按钮绑定点击事件
		
		updateBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//非空验证
				if(usernameTf.getText().trim()==null || usernameTf.getText().length()==0) {
					JOptionPane.showMessageDialog(null, "登录名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(oldpwdTf.getText().trim()==null || oldpwdTf.getText().length()==0) {
					JOptionPane.showMessageDialog(null, "旧密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!oldpwdTf.getText().trim().equals(MainFrame.getLoginedUser().getPassword())) {
					JOptionPane.showMessageDialog(null, "旧密码输入错误！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(newPwdTf.getText().trim()==null || newPwdTf.getText().length()==0) {
					JOptionPane.showMessageDialog(null, "新密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(newPwdTf.getText().trim().equals(MainFrame.getLoginedUser().getPassword())) {
					JOptionPane.showMessageDialog(null, "新密码不能和旧密码一样！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!repwdTf.getText().trim().equals(newPwdTf.getText().trim())) {
					JOptionPane.showMessageDialog(null, "两次密码输入不一致！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				Operator o = new Operator();
				o.setUsername(username);
				o.setPassword(newPwdTf.getText().trim());
				boolean isFlag = operatorService.updateOperator(o);
				if(isFlag==true) {
					JOptionPane.showMessageDialog(null, "密码修改成功！","提示",JOptionPane.INFORMATION_MESSAGE);				
					MainFrame.getMainFrame().dispose();					
					LoginFrame login = new LoginFrame();
					login.setVisible(true);
					setNull();
				}else {
					JOptionPane.showMessageDialog(null, "密码修改失败！","警告",JOptionPane.WARNING_MESSAGE);
				}
				
			}
		});
		panel_4.add(updateBtn);
		
		JButton resetBtn = new JButton("重置");
		resetBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setNull();
			}

		});
		panel_4.add(resetBtn);
		
		JLabel lblNewLabel = new JLabel("登  录  名：");
		panel.add(lblNewLabel);
		
		usernameTf = new JTextField();
		usernameTf.setEditable(false);
		panel.add(usernameTf);
		usernameTf.setColumns(20);
		getContentPane().setLayout(groupLayout);
		pack();
		setVisible(true);
		//获取登录名
		username = MainFrame.getLoginedUser().getUsername();
		
		usernameTf.setText(username);
		operatorService = CommonFactory.getOperatorService();
	}
	/**
	 * 清空数据
	 */
	private void setNull() {
		oldpwdTf.setText("");
		newPwdTf.setText("");
		repwdTf.setText("");
		
	}
}